//
//  PopperAnimation.swift
//  sxwb
//
//  Created by TianDu on 2016/11/15.
//  Copyright © 2016年 TianDu. All rights reserved.
//

import UIKit

class PopperAnimation: NSObject {

    var isPresented : Bool = false
    
    var presentFrame = CGRect.zero
    
    var callBack : ((_ presented:Bool)->())?
    
    override init() {
        
    }
    
    init(callBack:@escaping (_ presented:Bool)->()) {
        self.callBack = callBack
    }
    
}

// MARK: - 模态弹出效果
extension PopperAnimation : UIViewControllerTransitioningDelegate,UIViewControllerAnimatedTransitioning{
    
    /// 模态弹出效果
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController?{
        
        let presentController = WBPresentationController(presentedViewController: presented, presenting: presenting)
        
        presentController.presentFrame = presentFrame
        
        return presentController
    }
    /// 动画弹出效果
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        isPresented = true
        
        callBack!(isPresented)
        return self
    }
    /// - 动画消失效果
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        isPresented = false
        callBack!(isPresented)
        return self
    }
    
    /// 动画出现和消失的代理方法
    /// 动画执行的时间
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.5
    }
    /// 获取转场上下文:可以通过转场上下文获取弹出的view和消失的view
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        
        isPresented ? animationForPresentView(transitionContext) : animationForDismissView(transitionContext)
    }
    // 弹出的动画
    func animationForPresentView(_ transitionContext: UIViewControllerContextTransitioning) {
        
        // 1.获取弹出的view
        // .from 获取消失的view
        // .to   获取弹出的view
        let presentedView = transitionContext.view(forKey: .to)
        
        // 2.将弹出的view添加到containView里面
        transitionContext.containerView.addSubview(presentedView!)
        
        // 3.执行动画
        presentedView?.transform = CGAffineTransform(scaleX: 1.0,y: 0.0)
        // 设置锚点
        presentedView?.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
        // self.transitionDuration(using: transitionContext) 获取动画的时间
        UIView.animate(withDuration: self.transitionDuration(using: transitionContext), animations: {() -> Void in
            
            presentedView?.transform = CGAffineTransform.identity
            
        }, completion: {(_) -> Void in
            
            //必须告诉上下文,动画已完成
            transitionContext.completeTransition(true)
        })
    }
    
    //消失的动画
    func animationForDismissView(_ transitionContext: UIViewControllerContextTransitioning) {
        
        // 1.获取消失的view
        // .from 获取消失的view
        // .to   获取弹出的view
        let dismissView = transitionContext.view(forKey: .from)
        
        // self.transitionDuration(using: transitionContext) 获取动画的时间
        UIView.animate(withDuration: self.transitionDuration(using: transitionContext), animations: {() -> Void in
            
            // 2.执行动画
            dismissView?.transform = CGAffineTransform(scaleX: 1.0,y: 0.0001)
            
        }, completion: {(_) -> Void in
            
            dismissView?.removeFromSuperview()
            //必须告诉上下文,动画已完成
            transitionContext.completeTransition(true)
        })
        
    }
}
